package org.jeeasy.common.doc.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

import static org.jeeasy.common.core.config.constant.CommonConstant.X_ACCESS_TOKEN;


/**
 * @Author scott
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class SwaggerConfig implements WebMvcConfigurer {

    // 获取当前项目版本
    @Value("${jeeasy.version}")
    String version;

    private final MappingJackson2HttpMessageConverter jackson2HttpMessageConverter;

    //private static final String ACCESS_TOKEN = CommonConstant.X_ACCESS_TOKEN;

    @Bean
    @Lazy
    public OpenAPI springShopOpenApi() {
        return new OpenAPI().info(info())
                .components(components())
                .addSecurityItem(new SecurityRequirement().addList(X_ACCESS_TOKEN));
        //.externalDocs(new ExternalDocumentation().url("https://www.jeeasy.cn"));
    }

    private Components components() {
        return new Components().addSecuritySchemes(X_ACCESS_TOKEN, new SecurityScheme()
                .type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")
                .in(SecurityScheme.In.HEADER)
                .name(X_ACCESS_TOKEN))
                //.addSchemas("LocalDate", new Schema<LocalDate>().type("integer").format("int64"))
                ;
    }

    private Info info() {
        return new Info().title("Jeeasy API")
                .description("Jeeasy 快速开发脚手架")
                .version(version);
    }


    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 解决swagger3响应问题
        converters.add(new ByteArrayHttpMessageConverter());
        // json序列化
        converters.add(jackson2HttpMessageConverter);
        // xml序列化
        //XmlMapper xmlMapper = new XmlMapper.Builder(new XmlMapper())
        //        .enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION)
        //        .defaultUseWrapper(false)
        //        .build();
        //MappingJackson2XmlHttpMessageConverter mxlConverter = new MappingJackson2XmlHttpMessageConverter(xmlMapper);
        //converters.add(mxlConverter);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
